package com.vancouver.knows.business.tracker;

import com.pangu.bean.PageableResult;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.pangu.util.StringUtil;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.UserPostInfo;
import com.vancouver.knows.business.service.impl.GetUserPostsHandler;
import com.vancouver.knows.business.task.mark.GetUserPostsTaskMark;

import java.util.List;
import java.util.Map;

/**
 * Created by Chris Li on 2016-07-22.
 */
public class GetUserPostsTracker extends ABInvokeTracker {

    public GetUserPostsTracker(BusinessModule module, IResultReceiver resultReceiver) {
        super(module, resultReceiver);
    }

    @Override
    public String TAG() {
        return GetUserPostsTracker.class.getSimpleName();
    }

    @Override
    protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
        GetUserPostsTaskMark getUserPostsTaskMark = (GetUserPostsTaskMark) taskMark;
        if (getUserPostsTaskMark.getPage().equals(GetUserPostsTaskMark.PAGE_HISTORY) && StringUtil.isEmptyString(getUserPostsTaskMark.getStatus())) {
            if (module.getCacheManager().getUserPostHistoryPublishList(getUserPostsTaskMark.getUserId()) != null) {
                module.getCacheManager().getUserPostHistoryPublishList(getUserPostsTaskMark.getUserId()).clear();
            }
        } else if (getUserPostsTaskMark.getPage().equals(GetUserPostsTaskMark.PAGE_HISTORY) && !StringUtil.isEmptyString(getUserPostsTaskMark.getStatus())) {
            module.getCacheManager().getUserPostHistoryDraftList().clear();
        } else if (getUserPostsTaskMark.getPage().equals(GetUserPostsTaskMark.PAGE_FOLLOWING)) {
            if (module.getCacheManager().getUserPostFollowingList(getUserPostsTaskMark.getUserId()) != null) {
                module.getCacheManager().getUserPostFollowingList(getUserPostsTaskMark.getUserId()).clear();
            }
        }
    }

    @Override
    public void handleResult(OperateResult result) {
        PageableResult pageableResult = (PageableResult) result.getResultData();
        if (pageableResult.getContent() != null) {
            GetUserPostsTaskMark getUserPostsTaskMark = (GetUserPostsTaskMark) result.getTaskMark();
            Map<String, Object> map = (Map<String, Object>) pageableResult.getContent();
            List<UserPostInfo> newUserPostInfo = (List<UserPostInfo>) map.get(GetUserPostsHandler.KEY_USER_POSTS);
            if (getUserPostsTaskMark.getPage().equals(GetUserPostsTaskMark.PAGE_HISTORY) && StringUtil.isEmptyString(getUserPostsTaskMark.getStatus())) {
                List<UserPostInfo> userPostInfoCache = module.getCacheManager().getUserPostHistoryPublishList(getUserPostsTaskMark.getUserId());
                if (userPostInfoCache == null) {
                    module.getCacheManager().putUserPostHistoryPublishList(getUserPostsTaskMark.getUserId(), newUserPostInfo);
                } else {
                    userPostInfoCache.addAll(newUserPostInfo);
                    module.getCacheManager().putUserPostHistoryPublishList(getUserPostsTaskMark.getUserId(), userPostInfoCache);
                }
            } else if (getUserPostsTaskMark.getPage().equals(GetUserPostsTaskMark.PAGE_HISTORY) && !StringUtil.isEmptyString(getUserPostsTaskMark.getStatus())) {
                List<UserPostInfo> userPostInfoCache = module.getCacheManager().getUserPostHistoryDraftList();
                userPostInfoCache.addAll(newUserPostInfo);
                module.getCacheManager().setUserPostHistoryDraftList(userPostInfoCache);
            } else if (getUserPostsTaskMark.getPage().equals(GetUserPostsTaskMark.PAGE_FOLLOWING)) {
                List<UserPostInfo> userPostInfoCache = module.getCacheManager().getUserPostFollowingList(getUserPostsTaskMark.getUserId());
                if (userPostInfoCache == null) {
                    module.getCacheManager().putUserPostFollowingList(getUserPostsTaskMark.getUserId(), newUserPostInfo);
                } else {
                    userPostInfoCache.addAll(newUserPostInfo);
                    module.getCacheManager().putUserPostFollowingList(getUserPostsTaskMark.getUserId(), userPostInfoCache);
                }
            }
            getUserPostsTaskMark.setPageInfo(pageableResult.getPageInfo());
        }
    }
}
